package edu.cmu.cs.webapp.ioms.actions.order;

import org.eclipse.jface.action.IAction;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.wizard.WizardDialog;
import org.eclipse.ui.IViewActionDelegate;
import org.eclipse.ui.IViewPart;

import edu.cmu.cs.webapp.ioms.dao.DataAccessException;
import edu.cmu.cs.webapp.ioms.dao.impl.ClientDAOImpl;
import edu.cmu.cs.webapp.ioms.models.Client;
import edu.cmu.cs.webapp.ioms.ui.wizards.order.CustomerWizard;
import edu.cmu.cs.webapp.ioms.views.CustomersListView;

public class NewCustomerrViewAction implements IViewActionDelegate {
	private CustomersListView _view;

	@Override
	public void init(IViewPart view) {
		this._view = (CustomersListView) view;

	}

	@Override
	public void run(IAction action) {
		CustomerWizard wizard = new CustomerWizard(false, null);
		wizard.setWindowTitle("Add Customer");
		WizardDialog dialog = new WizardDialog(_view.getViewSite().getShell(),
				wizard);
		dialog.setBlockOnOpen(true);
		dialog.open();

		int returnCode = dialog.getReturnCode();

		if (returnCode == org.eclipse.jface.window.Window.OK) {
			// create new client
			Client client = wizard.getModel();
			try {
				new ClientDAOImpl().addClient(client);
			} catch (DataAccessException e) {
				// TODO
				e.printStackTrace();
			}

		} else {
		}

		this._view.refreshCustomersView();
	}

	@Override
	public void selectionChanged(IAction action, ISelection selection) {
		// TODO Auto-generated method stub

	}

}
